package com.jialin.rocket.controller;

import com.jialin.rocket.service.model.PayProducer;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lin.jia
 */
@Log
@RestController
@RequestMapping
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SendDemo {

    private final PayProducer payProducer;

    private static final String TOPIC = "jiaLin_pay_test_topic";

    /**
     * 模拟支付接口回调
     *
     * @return
     */
    @RequestMapping("api/v1/pay/pay_cb")
    public String callback(String text) {
        Message message = new Message(TOPIC, "tag", ("hello.jiaLin.rocketMq" + text).getBytes());
        DefaultMQProducer producer = payProducer.getProducer();
        try {
            SendResult result = producer.send(message);
            log.info("结果{}" + result);
        } catch (Exception e) {
            log.info("生产失败");
            e.printStackTrace();
        }
        return null;
    }
}
